﻿using System;
using Common.Annotations;
using Newtonsoft.Json;

namespace Common
{
    /// <summary>
    /// Обертка над JSON-конвертером. Хранит настройки формата
    /// </summary>
    public static class JsonConvertWrapper
    {
        private static readonly JsonSerializerSettings JsonSerializationSettings;

        static JsonConvertWrapper()
        {
            JsonSerializationSettings = new JsonSerializerSettings
                                            {
                                                NullValueHandling = NullValueHandling.Ignore,
                                                MissingMemberHandling = MissingMemberHandling.Ignore,
                                                DateTimeZoneHandling = DateTimeZoneHandling.Utc
                                            };
        }

        public static string Serialize([NotNull] object obj)
        {
            //if (obj == null) throw new ArgumentNullException("obj");
            return JsonConvert.SerializeObject(obj, Formatting.Indented, JsonSerializationSettings);
        }

        public static T Deserialize<T>([NotNull] string json)
        {
            if (json == null) throw new ArgumentNullException("json");
            return JsonConvert.DeserializeObject<T>(json, JsonSerializationSettings);
        }
    }
}